package com.whq.thrift.api.web.admin.db.requestlog;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

public interface RequestLogMapper {

	@Select("select * from t_request_log_#{tableSeq} where id = #{id}")
	@Results(value = { @Result(property = "id", column = "id"), @Result(property = "time", column = "time"), @Result(property = "phone", column = "phone"),
			@Result(property = "mid", column = "mid"), @Result(property = "snda_id", column = "snda_id"), @Result(property = "ip", column = "ip"),
			@Result(property = "device_id", column = "device_id"), @Result(property = "network", column = "network"),
			@Result(property = "mac_address", column = "mac_address"), @Result(property = "flow_id", column = "flow_id"),
			@Result(property = "request_params", column = "request_params"), @Result(property = "verify_code", column = "verify_code"),
			@Result(property = "create_time", column = "create_time"), @Result(property = "method", column = "method"),
			@Result(property = "app_version", column = "app_version"), @Result(property = "os_type", column = "os_type"),
			@Result(property = "os_version", column = "os_version"), @Result(property = "device_model", column = "device_model"),
			@Result(property = "device_mode2", column = "device_mode2"), @Result(property = "device_mode3", column = "device_mode3"),
			@Result(property = "device_mode4", column = "device_mode4"), 
			@Result(property = "device_mode5", column = "device_mode5")
	
			, @Result(property = "cost", column = "cost")
			, @Result(property = "result_code", column = "result_code")
	})
	public RequestLog getRecordByKey(@Param("tableSeq") int tableSeq, @Param("id") String id);

	@SelectProvider(type = RequestLogSqlProvider.class, method = "getSelectRecordsSql")
	@Results(value = { @Result(property = "id", column = "id"), @Result(property = "time", column = "time"), @Result(property = "phone", column = "phone"),
			@Result(property = "mid", column = "mid"), @Result(property = "snda_id", column = "snda_id"), @Result(property = "ip", column = "ip"),
			@Result(property = "device_id", column = "device_id"), @Result(property = "network", column = "network"),
			@Result(property = "mac_address", column = "mac_address"), @Result(property = "flow_id", column = "flow_id"),
			@Result(property = "request_params", column = "request_params"), @Result(property = "verify_code", column = "verify_code"),
			@Result(property = "create_time", column = "create_time"), @Result(property = "method", column = "method"),
			@Result(property = "app_version", column = "app_version"), @Result(property = "os_type", column = "os_type"),
			@Result(property = "os_version", column = "os_version"), @Result(property = "device_model", column = "device_model"),
			@Result(property = "device_mode2", column = "device_mode2"), @Result(property = "device_mode3", column = "device_mode3"),
			@Result(property = "device_mode4", column = "device_mode4"), @Result(property = "device_mode5", column = "device_mode5")
	
			, @Result(property = "cost", column = "cost")
			, @Result(property = "result_code", column = "result_code")
	})
	public List<RequestLog> findRecordsByMap(Map<String, Object> whereParams);

	@SelectProvider(type = RequestLogSqlProvider.class, method = "getSelectCountSql")
	public int findRecordsCount(Map<String, Object> whereParams);

	@SelectProvider(type = RequestLogSqlProvider.class, method = "getStatByMethodCodeSql")
	@Results(value = { @Result(property = "method", column = "method"),
			@Result(property = "verify_code", column = "verify_code"),
			@Result(property = "count", column = "count") })
	public List<RequestLog> statByMethodCode(Map<String, Object> whereParams);

	@SelectProvider(type = RequestLogSqlProvider.class, method = "getStatByCodeSql")
	@Results(value = { @Result(property = "verify_code", column = "verify_code"), @Result(property = "count", column = "count") })
	public List<RequestLog> statByCode(Map<String, Object> whereParams);

	@SelectProvider(type = RequestLogSqlProvider.class, method = "getIceCostTimeCountDataSql")
	@Results(value = { @Result(property = "ip", column = "ip"), 
			@Result(property = "method", column = "method"),
			@Result(property = "verify_code", column = "verify_code"), 
			@Result(property = "count", column = "count") })
	public List<RequestLog> queryGateWayCostTimeCountData(Map<String, Object> whereParams);

	@Update("REPLACE INTO t_stat_date_api_data (data_date, api, error_code, ip, request_count, create_time) "
			+ "SELECT #{statDay}, method, verify_code, ip, COUNT(*), CURRENT_TIMESTAMP FROM t_request_log_#{tableSeq} WHERE "
			+ "time>=#{startTime} AND time<#{endTime} GROUP BY method,verify_code,ip")
	public void replaceStatDateApiData(@Param("tableSeq") int tableSeq, @Param("statDay") String statDay, @Param("startTime") String startTime,
			@Param("endTime") String endTime);

	@Update("REPLACE INTO t_stat_hour_api_data (data_date, data_hour, api, error_code, ip, request_count, create_time) "
			+ "SELECT #{statDay}, #{statHour}, method,verify_code,ip, COUNT(*), CURRENT_TIMESTAMP FROM t_request_log_#{tableSeq} WHERE "
			+ "time>=#{startTime} AND time<#{endTime} GROUP BY method,verify_code,ip")
	public void replaceStatHourApiData(@Param("tableSeq") int tableSeq, @Param("statDay") String statDay, @Param("startTime") String startTime,
			@Param("endTime") String endTime, @Param("statHour") int statHour);
	
	@Delete("DELETE FROM t_stat_date_api_data WHERE create_time<#{thresholdTime}")
	public void delDataApiData(@Param("thresholdTime") String thresholdTime);
	
	@Delete("DELETE FROM t_stat_hour_api_data WHERE create_time<#{thresholdTime}")
	public void delHourApiData(@Param("thresholdTime") String thresholdTime);
	
}
